<?php
class IndexAction extends IntesysAction{
	public $db;
	public function index($layoutMode=null){
        $this->db=D('Member');
        $info=D('Info');
		$integral_rule=M('Integral_rule');
		$member_groupDB=M('Member_group');
		$integral_logDB=M('Integral_log');
		
		$target=MODULE_NAME.ACTION_NAME;
		$this->pages_layout=D('Pages_layout')->where(['title'=>$target])->find();
		//dump($this->siteInfo);
		$this->assign('siteInfo', $this->siteInfo);
		//查找用户有没有入职信息登记
		$this->particular2 = M('Mission') -> where(array('particular' => 2, 'member_id' => $this->memberInfo['id'])) -> find();
		if(!$this->particular2){
			$this->particular2formid = M('Form') -> where(array('particular' => 2,'site_id'=>__SITEID__)) -> getField('id');
		}
		//查询当前站点有没有部门
		$this->member_position=M('Member_position')->where(['site_id'=>__SITEID__])->count();
		if($layoutMode=='start'){
			$_REQUEST['layoutMode']='start';
		}
		
 		$this->display('Index:index'); 
	}
	public function fixTaskStatu(){
		if($this->_get('id')){
			if($this->task->taskDone()){
				$this->success('操作成功');	
			}else{
				$this->error($this->task->getError());
			};
		}
	}
	
	
	public function getranking(){
		$rdb = D('Integralranking');
		
		$list = M('Integral_log') -> where(array('site_id' => __SITEID__)) -> select();
		
		foreach($list as $v){
			$rdb -> getDataFromLog($v);
		}
	}
	
	
	//做月份趋势图格式数据
	public function makeMonthChart($monthlist){
		$arr=array();
		$monthDay=$this->getCurMonthDay();
		$month=date('Y-m-d',$monthDay[1]);
		//(先循环出一个月所有天数的数组)
		while($month!=date('Y-m-d',$monthDay[0])){
			$arr[$month]=array('plus'=>0,'minus'=>0,'time'=>$month);
			$month=date('Y-m-d',strtotime($month.' -1 day'));
		}
		$arr[date('Y-m-d',$monthDay[0])]=array('plus'=>0,'minus'=>0,'time'=>$month);
		$arr=array_reverse($arr);
		//(然后将数据加入数组内)
		foreach($monthlist as $vo){
			$month=date('Y-m-d',$vo['time']);
			$num=array();
			$arr[$month]=isset($arr[$month])?$arr[$month]:array();
			if($vo['changenum']>0){
				if(isset($arr[$month]['plus'])){
					$num=(int)$arr[$month]['plus']+(int)$vo['changenum'];
				}else{
					$num=(int)$vo['changenum'];
				}
				$arr[$month]['plus']=$num;
			}else{
				if(isset($arr[$month]['minus'])){
					$num=(int)$arr[$month]['minus']-(int)$vo['changenum'];
				}else{
					$num=(int)$vo['changenum'];
				}
				$arr[$month]['minus']=$num;
			}
		}
		return $arr;
	}
	
	//获取当前月份的第一天和最后一天（array(第一天，第二天)）
	private function getCurMonthDay(){
		return [strtotime(date('Y-m-01 00:00:00',time())),strtotime(date('Y-m-d 23:59:59',strtotime(date('Y-m-01 00:00:00',time()))).' +1 month -1 day')];	
	}
	//登录用户关联的管理后台
	public function manage(){
		redirect('/Intesys');
	}
	//获取积分系统一些重要的提示
	public function getSituation(){
		//部门管理员是否设置
		$positionCount=M('Member_position')->where(['site_id'=>__SITEID__,'member_id'=>0])->count();
		if($positionCount){
			$status=1;
			$data[]=['您好！当前积分管理系统有'.$positionCount.'个部门未设置管理员，为了您更好地使用系统，请立即设置！',U('Employee/index')];	
		}else{
			$status=0;	
		}
		die(json_encode(['status'=>$status,'tbody'=>$data]));
	}
}